package com.duck.communication.controller;

import com.duck.common.domain.PagerPojo;
import com.duck.common.domain.Result;
import com.duck.common.utils.OneContentProcessUtil;
import com.duck.common.utils.UserContext;
import com.duck.communication.domain.dto.PostInfoDto;
import com.duck.communication.domain.dto.PublishPostDto;
import com.duck.communication.domain.dto.UpdatePostDto;
import com.duck.communication.domain.vo.PostInfoVo;
import com.duck.communication.service.PostService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 用户发布帖子
     * @param post 所需参数
     * @return 结果
     */
    @PostMapping("/publishPost/")
    public Result publishPost(@RequestBody PublishPostDto post){
        //获取用户ID
        String userId = String.valueOf(UserContext.getUser());
        String coverUrl = post.getCoverUrl() != null ? post.getCoverUrl() : "https://interview-agent.oss-cn-beijing.aliyuncs.com/e06dc462-9378-4577-a75f-3119ec9ae707.png";
        //发帖
        HashMap<String, Object> info = new HashMap<>() {{
            put("userId", userId);
            put("title", post.getTitle());
            put("content", post.getContent());
            put("categoryId", post.getCategoryId());
            put("cover",coverUrl);
        }};
        Integer result = postService.publishPost(info);
        if (result == 1) {
            return new Result(200, "发布成功");
        }
        return new Result(404, "发布失败");
    }

    /**
     * 得到所有的帖子
     * @return 所有贴
     */
    @GetMapping("/getAllPost/")
    public Result getAllPost(@RequestParam Integer page,@RequestParam Integer size){
        PageHelper.startPage(page,size);
        List<HashMap<String,Object>> allPost = postService.getAllPost();
        PageInfo<HashMap<String,Object>> pageInfo = new PageInfo<>(allPost);
        PagerPojo pagerPojo = new PagerPojo(pageInfo.getTotal(),pageInfo.getList());
        return new Result(pagerPojo,200,"查询成功");
    }

    /**
     * 得到指定用户的所有帖子
     * @return 帖子
     */
    @GetMapping("/getPost/")
    public Result getPostByUserId(@RequestParam Integer page,@RequestParam Integer size){
        String userId = String.valueOf(UserContext.getUser());
        PageHelper.startPage(page,size);
        List<HashMap<String,Object>> post = postService.getPostByUserId(userId);
        PageInfo<HashMap<String,Object>> pageInfo = new PageInfo<>(post);
        PagerPojo pagerPojo = new PagerPojo(pageInfo.getTotal(),pageInfo.getList());
        return new Result(pagerPojo,200,"查询成功");
    }

    /**
     * 更新帖子
     * @param post 更新参数
     * @return 结果
     */
    @PostMapping("/updatePost/")
    public Result updatePost(@NotNull @RequestBody UpdatePostDto post){
        // 获取用户ID
        String userId = String.valueOf(UserContext.getUser());
        HashMap<String, Object> info = new HashMap<>() {{
            put("userId", userId);
            put("postId", post.getPostId());
            put("title", post.getNewTitle());
            put("content", post.getNewContent());
            put("categoryId", post.getNewCategoryId());
            put("coverUrl", post.getNewCoverUrl());
        }};
        Integer result = postService.updatePost(info);
        if (result == 1) {
            return new Result(200, "更新成功");
        }
        return new Result(404, "更新失败");
    }


    @GetMapping("/selectCategory/")
    public Result selectCategory(){
        List<HashMap<String,Object>> category=postService.selectCategory();
        return new Result(category,200,"查询成功");
    }

    @GetMapping("/postInfo/")
    public Result postInfo(@RequestParam String postId){
        //接数据库内容
        PostInfoDto postInfoDto = postService.searchPostInfo(postId);
        //拿到原始content字段
        String content = postInfoDto.getContent();
        //对原始字段做处理
        List<String> newContent = OneContentProcessUtil.processContent(content);
        //放回去
        PostInfoVo postInfoVo = new PostInfoVo();
        postInfoVo.setContent(newContent);
        postInfoVo.setCover(postInfoDto.getCover());
        postInfoVo.setTime(postInfoDto.getTime());
        postInfoVo.setTitle(postInfoDto.getTitle());
        postInfoVo.setType(postInfoDto.getType());
        postInfoVo.setUsername(postInfoDto.getUsername());
        postInfoVo.setUserId(postInfoDto.getUserId());
        return new Result(postInfoVo,200,"成功");
    }
}
